import { objDeepCopy,getDateString } from "@/assets/js/common.js";
let Controller = {
	mounted() {
		this.initdata();
	},
	activated() {
		this.initdata();
	},
	methods: {
		initdata() {
			this.Model.orderNumber = this.$route.query.id;		//订单id
			this.requestDetail(this,this.Model.orderNumber);
			this.getremarkReason();
		},
		//获取原因
		getremarkReason() {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whReturnReason/nolist',
				data:{}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					this.Model.remarkReason = result.map(item => {
						return {
							label: item.reason,
							value: item.reason
						}
					});
				}else{
					that.tips(that,'warning',data.msg)
				}
			});
		},
		//详情接口
		requestDetail(that,id) {
			this.$ajax({
				method: 'post',
				//url:'/api/supplier/whWarehouseDetail/outAndInDetails',
				url:this.apiTest+'/supplier/whWarehouseDetail/outAndInDetails',
				data:{
					condition:{
						number:id
					}
				}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					this.Model.whWarehouseId = result.whWarehouseId;
					this.Model.supplierInfoId = result.supplierInfoId;
					this.Model.outWarehouseId = result.outWarehouseId;
					console.log(result)
					//应出商品（已入库）
					that.Model.tableDataOut = objDeepCopy(result.shouldSaleGoodsInfoVos);
					this.Model.filterOption.items[0].value = result.supplierName;//供应商
					this.Model.filterOption.items[1].value = result.outWarehouseName;//出库仓库
					this.Model.filterOption.items[2].value = result.type;//退货单类型
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		//下拉框
		getReturnselect(that) {
			this.$ajax({
				method: 'get',
				url:this.apiTest+'/supplier/whWarehouse/getInOutDropBox?parentId=205&type=0',
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					 console.log(result)
					this.Model.filterOptionData = result;
					let supplierArr = [],locationArr = [],wareHouseTypeArr = [];
					for (let i in result.supplier) {//供应商
						supplierArr.push({ label: result.supplier[i], value: i });
					};
					for (let i in result.location) {//出库仓库
						locationArr.push({ label: result.location[i], value: i });
					};
					for (let i in result.wareHouseType) {//退货单类型
						wareHouseTypeArr.push({ label: result.wareHouseType[i], value: i });
					};
					this.Model.filterOption.items[0].data = supplierArr;//供应商
					this.Model.filterOption.items[1].data = locationArr;//出库仓库
					this.Model.filterOption.items[2].data = wareHouseTypeArr;//退货单类型
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		//请求扫码入库 
		skuEntryAjax(that,code){
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/supplierPurchasingOrderDetails/getPoOrderGoodsInfo',
				//url:'/api/supplier/whWarehouseDetail/getByKeyWord',
				data:{
					batchNo:code,
					supplierInfoId: that.Model.supplierInfoId,
					outWarehouseId: that.Model.outWarehouseId,
					whWarehouseId: that.Model.whWarehouseId
				}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var obj = {};
					var result = data.result;
					if(result){	
						console.log(result)
						for (var i = 0; i < result.length; i++) {				
							obj = objDeepCopy(result[i]);
							that.Model.tableDataOut.push(obj);
						}
					}
				}else{
		
				}
		
			});
		},
		
		//扫码入库
		checkSku(){
			var code = this.Model.writeSku;
			var tableDataOut = this.Model.tableDataOut;
			var codeIndexOf = this.valueInData(code,tableDataOut);
			if(codeIndexOf == -1){
				this.skuEntryAjax(this,code);
			}else{
				this.tips(this,'warning','条码已存在')
			}
		},
		//遍历 判断是否存在数组中
		valueInData(val,data){
			var indexof = -1;
			data.forEach(function(value,index,array){
				Object.keys(value).forEach(function(key){
					// console.log(key,value[key]);
					if(key == 'batchNo' && value[key] == val){
						indexof = 0;
						return;
					}
				});
			})
			return indexof;
		},
		//删除 表格数据
		deleteTableData(){
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tableDataOut = this.Model.tableDataOut;
			if(TableSelectData.length == 0){
				this.tips(this,'warning','请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				var delIdArr = [];//需要发起请求
				TableSelectData.forEach(function(value,index,array){
					tableDataOut.forEach(function(val,idx,arr){
						if(value == val){
							that.Model.tableDataOut.splice(idx,1);
							that.tips(that,'success','删除成功');
							
							//判断是否有在库里
							if(value.whWarehouseDetailId){
								delIdArr.push(value.whWarehouseDetailId);
							}
						}
					})
				});
				/* 若数组长度大于0，则发起请求删除*/
				if(delIdArr.length != 0){
					that.deleteAjax(that,delIdArr);
				}
			}).catch(() => {
			           
			});
		},
		//删除请求
		deleteAjax(that,params) {
			let whWarehouseId = this.Model.whWarehouseId;
			this.$ajax({
				method: 'put',
				url:this.apiTest+'/supplier/whAllot/deleteDetail/40/'+whWarehouseId,
				data:params
				//url:'/api/supplier/whWarehouseDetail/getByKeyWord',
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					that.tips(that,'success','删除成功');
				}	
			});
		},
		//保存、确认出库（编辑时）
		OutofstockHandle(type) {
			let params = {};
			params.supplierInfoId = this.Model.supplierInfoId;
			params.outWarehouseId = this.Model.outWarehouseId;
			params.id = this.Model.whWarehouseId;//代表进行的是编辑步骤	
			var mainTableData = this.Model.tableDataOut;				
			var arr = [];
			for (var i = 0; i < mainTableData.length; i++) {
				var obj = {
					goodsId:mainTableData[i].id,
					PoNumber:mainTableData[i].purchaseVoucherNo,
					remark:mainTableData[i].remark,
					mainQuantity:mainTableData[i].mainQuantity
				}
				arr.push(obj);
			}
			params.poNumberAndIdDtoList = objDeepCopy(arr);
			var Noresultarr = '';//无填写的数组
			mainTableData.forEach(item=>{
				if(!item.remark){
					Noresultarr += item.batchNo+'<br/>';			
				}
			});
			if(type == '确认出库'){
				params.status = '40';
				params.detailStatus = true;
				if(!Noresultarr==''){
					this.$alert('<h4>以下条码未填写</h4><p>'+Noresultarr+'</p>', '提示', {
						dangerouslyUseHTMLString: true
					});
				}else{
					this.OutofstockAjax(params);
				}
			}else{
				params.status = '30';
				params.detailStatus = false;
				if(!Noresultarr==''){
					this.$alert('<h4>以下条码未填写</h4><p>'+Noresultarr+'</p>', '提示', {
						dangerouslyUseHTMLString: true
					});
				}else{
					this.OutofstockAjax(params);
				}
			}
			
		},
		//保存、确认出库请求（编辑时）
		OutofstockAjax(params) {
			this.$ajax({
				method: 'post',
				url:this.apiTest+'/supplier/whWarehouse/savePurchaseReturn',
				//url:'/api/supplier/whWarehouseDetail/getByKeyWord',
				data:params
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					console.log(result)
				}	
			});
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
			console.log(val);
		},
		showChild(val) {
			console.log(this.Model.searchFromData)
		},
		toSearch(val) {
			console.log(val)
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
			console.log(val);
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
	}
}
export default Controller;
